<?php
header('content-type:text/html;charset=utf-8');
$host = "localhost";
$user = "root";
$password = "";
$db = "library";


$mysqli = new mysqli($host, $user, $password, $db); //实例化mysqli对象，连接mysql数据库
if ($mysqli->connect_errno) {
    die($mysqli->connect_error);
}
$mysqli->set_charset('utf8'); //设置字符集
register($mysqli);
$mysqli->close();


//用户注册，插入新数据
function register($mysqli)
{
    $sql = "insert into book(number,bookname,aythor) values(?,?,?)";
    $mysqli_stmt = $mysqli->prepare($sql); //准备预处理语句

    $number = $_POST["number"];
    $bookname = $_POST["bookname"];
    $aythor = $_POST["aythor"];


    //s代表string类型
    $mysqli_stmt->bind_param('iss', $number, $bookname,$aythor);


    if($number == '') {
        
        echo "<script>alert('请输入编号！');window.location.href='insert.html'</script>";
    }
   
    else if($bookname == '') {
        
        echo "<script>alert('请输入书名！');window.location.href='insert.html'</script>";
    }
    else if($aythor == '') {
        
        echo "<script>alert('请输入作者！');window.location.href='insert.html'</script>";
    }
    //执行预处理语句
    else if ($mysqli_stmt->execute()) {
        // echo $mysqli_stmt->insert_id; //程序成功，返回插入数据表的行id
        echo "<script>alert('恭喜您，添加成功！');window.location.href='insert.html'</script>";
    }  
    else {
        
        echo "<script>alert('编号输入重复！');window.location.href='insert.html'</script>";
    }
    //释放结果集
    $mysqli_stmt->free_result();
    $mysqli_stmt->close();
}
